<?php
/**
 * 发送邮件
 * @param unknown $mailto
 * @param unknown $mailtoname
 * @param unknown $mailTitle
 * @param unknown $mailContent
 * @return string
 */
function  sendMail($mailto,$mailtoname,$mailTitle,$mailContent){
	//设置超时时间，0为无限制
	set_time_limit(0);
	
	//加载PHPmailer文件
	require COMMON_PATH . 'Lib/PHPMailerAutoload.php';
	
	$mail =new PHPMailer(); 
	$mail->IsSMTP();  // 设置用smtp协议去发送邮件
	$mail->SMTPDebug=0;   
	$mail->Host = C('MAIL_HOST');   // smtp邮件服务器的地址  
	$mail->SMTPAuth = C('MAIL_SMTP');           //是否用smtp账户认证  
	$mail->Username = C('MAIL_USERNAME');//smtp邮件服务器的账号  
	$mail->Password = C('MAIL_PASSWORD'); //smtp邮件服务器的密码  
	$mail->From = C('MAIL_FROM');//发件人邮箱地址
	$mail->FromName =C('MAIL_FROMNAME');             //发件人名称
	//$mail->SMTPSecure="SSL";
	$mail->CharSet = C('MAIL_ENCODE'); //设置文档字符编码
	$mail->Encoding = "base64"; //设置编码   
	
	$mail->addAddress($mailto,$mailtoname);  // 设置收件人的邮箱地址
	$mail->addReplyTo(C('MAIL_FROM'),C('MAIL_FROMNAME')); //设置回复邮件地址   
	
	//$mail->WordWrap = 50; // 设置换行字符数量   
	//$mail->addAttachment("p1.jpg"); // 添加附件 
	//$mail->AddAttachment("/tmp/image.jpg", "new.jpg");    
	$mail->isHTML(true);  //设置发送html格式内容    
	//   
	$mail->Subject = $mailTitle;    
	//    
	$mail->Body = $mailContent;
	
	 //$mail->AltBody ="反对撒地方gfadjkfdskjfdsa "; 
	//发送函数Send()   
	if(!$mail->Send())    
	{    
		return "失败信息: " . $mail->ErrorInfo;    
		exit;    
	}    
	else {    
		return 'success';
	}   
}


/**
 *  PHP随机字符串的代码
 * @return Ambigous <unknown, string>
 */
function randomkeys()
{
   $length=5;
   $pattern ="1234567890abcdefghijklmnopqrstuvwxyz";
   $key  = $pattern[rand(0,35)];
   for($i=1;$i<$length;$i++)
   {
       $key .= $pattern[rand(0,35)];
   }
  return $key;
}



/**
 * 根据偏移量和每页显示的记录数量来查询数据库取得当前页的数据
	$getNewssql="select {$column} from {$table} order by {$orderby} {$orderbycounm} limit {$offset},{$perpage} ";
	$newArray=select($getNewssql);
	foreach($newArray as $key =>$newValue)
	{
		$newArray[$key]['content']=mb_substr(strip_tags($newArray[$key]['content']),0,12,'utf8');
		$newArray[$key]['title']=mb_substr(strip_tags($newArray[$key]['title']),0,12,'utf8');
	}
	
	
	$getcountAll="select count(*) as countAll from {$table}";
	$countRows=getOne($getcountAll);
 * Enter description here ...
 * @param unknown_type $table
 * @param unknown_type $currpage
 * @param unknown_type $column
 * @param unknown_type $perpage
 * @param unknown_type $showpageAll
 * @param unknown_type $orderby
 * @param unknown_type $orderbycounm
 */
function pages($currpage,$countRows,$perpage=4,$showpageAll=5)
{

	//计算总页数 ceil函数向上取整 floor向下取整  round()四舍五入
	$pageAll=ceil($countRows['countAll']/$perpage);
	//判断是否前端有传页码过来 如果有就取出来 没有就是默认值1
	//计算取数据的偏移量 从哪个位置开始取数据
	$offset=($currpage-1)*$perpage;
	
	//控制显示页码数量
	//定义显示页码的总数量
	if($showpageAll>$pageAll)
	{
		$showpageAll=$pageAll;
	}
	//判断当页码小于偏移量 从1开始显示页码
	if($currpage<ceil($showpageAll/2)){
		$showfirst=1;
	}
	//判断页码大于（总页码-偏移量） 从（总页码-显示页码总数+1）开始显示页码
	else if($currpage>$pageAll-floor($showpageAll/2)){
		$showfirst=$pageAll-$showpageAll+1;
		if($showfirst<=0){
			$showfirst=1;
		}
	}
	else{
		//从（当前页码-偏移量+1）开始显示页码 把当前页码计算 放到中间
		$showfirst=$currpage-floor($showpageAll/2);
	}
	if($showfirst<=0){
			$showfirst=1;
	}
	$data=array(
		"currpage"=>$currpage,
		"pageAll"=>$pageAll,
		"showfirst"=>$showfirst,
		"showpageAll"=>$showpageAll,
		"offset"=>$offset,
		"perpage"=>$perpage
	);
	return $data;
}


/**
 * 循环出当前分类的所有父类信息出来，包括当前分类的信息
 * @param unknown $cateID
 * @return multitype:Ambigous <\Think\mixed, boolean, NULL, multitype:, mixed, unknown, string, object>
 */
function getParentCate($cateID)
{
	$cateArray=array();
	$parentId=$cateID;
	while($parentId>0)
	{
		$pAray=D("category")->where('id='.$parentId)->find();
		$parentId=$pAray['parent_id'];
		$cateArray[]=$pAray;
	}
	return $cateArray;

}
